<?php
/**
 * User: i@fedax.cn
 * Time: 2018/7/27 17:45
 */

namespace app\admin\model;


class PersonalRoom extends DataBase
{
    protected $autoWriteTimestamp = true;

    public function getUpdateTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public function getDataList()
    {
        $page_config = array(
            'page' => input('offset',1),
            'limit' => input('limit',10)
        );
        $order = input('sort', 'a.id') . ' ' . input('order', 'desc');
        $where = []; //查询条件
        if (!empty(input('start_time')) && !empty(input('end_time'))) {
            $start = strtotime(input('start_time'));
            $end = strtotime(input('end_time'));
            $where[] = ['a.create_time', 'between', [$start, $end]];
        }
        if (!empty(input('value')) && !empty(input('key'))) {
            $value = input('value');
            $key = input('key');
            if ($key == 'r_uid') {
                $r_id = User::where('u_id','=',$value)->value('id');
                $where[] = ['c.r_id', '=',$r_id];
            }
            if ($key == 'h_uid') {
                $where[] = ['b.u_id','=', $value];
            }
            //halt($where);
        }

        $data =  $this
            ->alias('a')
            ->field('a.*,b.u_id,b.nickname,c.r_id,c.r_name')
            ->join('users b','a.user_id = b.id','left')
            ->join('rooms c','a.room_id = c.id','left')
            ->order($order)
            ->where($where)
            ->paginate($page_config);

        foreach($data as $key => $value){
            $data[$key]['r_head'] = User::where(['id'=>$value['r_id']])->value('u_id'); //房主ID
            $data[$key]['r_nickname'] = User::where(['id'=>$value['r_id']])->value('nickname'); //房主昵称
        }
        return $data;
    }

    /**
     * @return array
     * @user 帅帅吴
     * @date 2018/12/30
     * @title 个人房间删除
     */
    public function personalRoomDel()
    {
        $id = input('get.id');
        $re = $this->where('id','=',$id)->delete();
        if($re) return ['code'=>100,'msg'=>'删除成功'];
        else return ['code'=>-100,'msg'=>'删除失败'];
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @user 帅帅吴
     * @title 个人房间添加
     */
    public function personalRoomAdd()
    {
        $newData = input('post.');

        $user_id = User::where('u_id','=',$newData['u_id'])->value('id');
        if(!$user_id) return ['code'=>-100,'msg'=>'没找到该房主'];

        $r_id = User::where('u_id','=',$newData['room_id'])->value('id');
        if(!$r_id) return ['code'=>-100,'msg'=>'没找到该厅主'];

        $room_id = Room::where('r_id','=',$r_id)->value('id');
        if(!$room_id) return ['code'=>-100,'msg'=>'没找到该房间'];

        $re = $this->where('room_id','=',$room_id)->find();
        if($re) return ['code'=>-100,'msg'=>'该房间已使用'];

        $newData['user_id'] = $user_id;
        $newData['room_id'] = $room_id;
        $res = $this->save($newData);

        if($res) return ['code'=>100,'msg'=>'添加成功'];
        else return ['code'=>-100,'msg'=>'添加失败'];
    }
    /**
     * Notes: 个人房间流水数据
     * User: 王俊成
     * Date: 2018/11/6
     * Time: 17:32
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function personalRoomFlowingData(){
        $pageConfig = [
            'page' => input('get.pageNum',1),
            'list_rows' => input('get.limit',10)
        ];
        $startTime = input('get.start_time');
        $endTime = input('get.end_time');
        $keyWords = input('get.keyWords');
        $map = [];//个人房间信息查询条件
        if(!empty($keyWords) || $keyWords == '0'){
            $key = input('get.key');
            if($key == 'room_uid'){
                $user_id = User::where('u_id',$keyWords)->value('id');
                if($user_id){
                    $room_id = Room::where('r_id',$user_id)->value('id');
                    if($room_id)
                        $map[] = ['room_id','=',$room_id];
                    else
                        $map[] = ['id','=',0];
                }
                else
                    $map[] = ['id','=',0];
            }
            if($key == 'hall_uid')
                $map[] = ['user_id','=',$keyWords];

        }
        //   $where = [];//总流水查询条件
        if(!empty($startTime) && !empty($endTime)){
            $startTime = strtotime($startTime);
            $endTime = strtotime($endTime);
            if($startTime <= $endTime)
                $map[] = ['create_time','between',[$startTime,$endTime]];
            else
                $map[] = ['create_time','between',[$endTime,$startTime]];
        }
        else if(!empty($startTime)){
            $startTime = strtotime($startTime);
            $map[] = ['create_time','>=',$startTime];
        }
        else if(!empty($endTime)){
            $endTime = strtotime($endTime);
            $map[] = ['create_time','<=',$endTime];
        }
        //dump($map);
        $data = $this->where($map)->order('create_time DESC')->paginate($pageConfig);
        if($data){
            foreach($data as $key => $value){
                $value['scale'] = $value['scale'].'%';
                $value['room_name'] = get_room_name($value['room_id']);
                $room_user_id = Room::where('id',$value['room_id'])->value('r_id');//房主的user_id
                $value['room_uid'] =  get_uid($room_user_id);//房主ID
                $value['hall_uid'] = get_uid($value['user_id']);//厅主ID
                $value['room_nickname'] = get_user_name($room_user_id);//房主昵称
//                $where[] = ['room_id','=',$value['room_id']];
//                $value['flowing_num'] = GiftRecord::where($where)->sum('gift_price');
//                array_pop($where);//将room_id条件删除
                $data[$key] = $value;
            }
        }
        return $data;
    }

    /**
     * Notes: 个人房间上周流水数据导出
     * User: 王俊成
     * Date: 2018/11/6
     * Time: 18:09
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function personalRoomFlowingExport(){
        $startTime = strtotime(date('Y-m-d',strtotime('last week')));
        $endTime = $startTime + 7 * 24 * 3600;
        //获取数据
        $data = $this->alias('a')->fieldRaw('a.user_id,a.room_id,alipay_card,alipay_name,scale,a.create_time,SUM(gift_price) as flowing_num')
            ->leftJoin('gift_record b','a.room_id=b.room_id')->where('b.create_time','between',[$startTime,$endTime])
            ->group('b.room_id')->order('flowing_num DESC')->having('SUM(gift_price)>0')->select();
        if($data){
            $k = 1;
            foreach($data as $key => $value){
                $value['room_name'] = get_room_name($value['room_id']);
                $value['hall_uid'] = get_uid($value['user_id']);
                $value['type'] = '个人流水厅';
                $value['scale'] = $value['scale'].'%';
                $value['money'] = $value['flowing_num'] * 0.1 * $value['scale']/100;
                $value['whole_money'] =round( $value['flowing_num'] * 0.1 * $value['scale']/100 );
                $value['order'] =$k;
                $k++ ;
                for($i = 0;$i < 7;$i++){
                    $dates = date('Y-m-d',$startTime + $i * 24 * 3600);
                    $value[$dates] = GiftRecord::where([['room_id','=',$value['room_id']],['create_time','egt',strtotime($dates)],['create_time','lt',strtotime($dates.' 23:59:59')]])
                        ->sum('gift_price');
                }
                $data[$key] = $value;
            }
        }
        $fileName = '个人房间流水';
        $remarkData = ['个人房间流水'];
        $cellTitle = [
            ['order','序号'],
            ['room_name','房间名'],
            ['hall_uid','厅主ID'],
            ['type','房间属性'],
            ['alipay_name','支付宝姓名'],
            ['alipay_card','支付宝账号'],
            ['scale','返现比例'],
            ['flowing_num','上周流水'],
            ['money','金钱'],
            ['whole_money','金钱(求整)'],
            ['create_time','创建时间']
        ];
        for($i = 0;$i < 7;$i++){
            $dates = date('Y-m-d',$startTime + $i * 24 * 3600);
            $cellTitle[] = [$dates,$dates];
        }
        $excelObj = new \app\common\api\Excel();
        $excelObj->exportExcel($fileName,$remarkData,$cellTitle,$data);
    }
}